/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Formatka.java
 *
 * Created on 2011-06-09, 12:45:38
 */
package formatki;

import bledy.BladGrafu;
import bledy.BladKonca;
import bledy.BladPoczatku;
import graf.DefAlgorytm;
import graf.DefUse;
import graf.Definicja;
import graf.DuParyAlgorytm;
import graf.Graf;
import graf.KrawedzioweAlgorytm;
import graf.ParKrawedzioweAlgorytm;
import graf.Sciezka;
import graf.SciezkiDoskonaleAlgorytm;
import graf.SciezkiProsteAlgorytm;
import graf.TestyDefAlgorytm;
import graf.TestyDuParyAlgorytm;
import graf.TestyKrawedzioweAlgorytm;
import graf.TestyParKrawedzioweAlgorytm;
import graf.TestySciezkiDoskonaleAlgorytm;
import graf.TestySciezkiProsteAlgorytm;
import graf.TestyUzycieAlgorytm;
import graf.TestyWierzcholkoweAlgorytm;
import graf.Uzycie;
import graf.UzycieAlgorytm;
import graf.Wierzcholek;
import graf.WierzcholekPlusPlus;
import graf.WierzcholkoweAlgorytm;
import java.awt.FileDialog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;

/**
 *
 * @author marek
 */
public class Formatka extends javax.swing.JFrame {

    Graf graf;
    WidokGrafu widokGrafu;
    DefinicjeUzycia defuse;

    public WidokGrafu getWidokGrafu() {
        return widokGrafu;
    }

    /** Creates new form Formatka */
    public Formatka() {
        initComponents();
        setLayout(null);
        widokGrafu = new WidokGrafu(this);
        widokGrafu.setBounds(10, 10, 480, 500);
        widokGrafu.setVisible(true);
        jPanel6.add(widokGrafu);
        defuse = new DefinicjeUzycia(this);
        defuse.setVisible(false);
        this.aktywujPrzyciski(false);
    }

    public Graf getGraf() {
        return graf;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaKrawedzie = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfPocz = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jtfKon = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtaWynik = new javax.swing.JTextArea();
        jbTworzGraf = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTSciezkiTesty = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jLabel1.setText("Wpisz krawędzie grafu,");

        jLabel2.setText("podając pary wierzchołków");

        jLabel3.setText("w osobnych liniach");

        jtaKrawedzie.setColumns(20);
        jtaKrawedzie.setRows(5);
        jScrollPane1.setViewportView(jtaKrawedzie);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(28, 28, 28)))))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jLabel4.setText("Wpisz wierzchołki");

        jLabel5.setText("odzielone spacjami");

        jLabel6.setText("początkowe,");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtfPocz, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(5, 5, 5)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(jtfPocz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jLabel7.setText("Wpisz wierzchołki");

        jLabel8.setText("końcowe,");

        jLabel9.setText("odzielone spacjami");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtfKon, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jtfKon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jLabel10.setText("Wynik");

        jtaWynik.setColumns(20);
        jtaWynik.setRows(5);
        jScrollPane2.setViewportView(jtaWynik);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(jLabel10))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addContainerGap())
        );

        jbTworzGraf.setText("Tworz graf");
        jbTworzGraf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTworzGrafActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jButton2.setText("Wymagania wierzcholkowe");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Wymagania krawedzowe");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Wym. par krawedzi");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Ścieżki proste");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Ścieżki doskonałe");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Definicje");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Użycia");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Du-pary");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addGap(4, 4, 4)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton8)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 255, 0)));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 510, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 501, Short.MAX_VALUE)
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)));

        jTSciezkiTesty.setColumns(20);
        jTSciezkiTesty.setRows(5);
        jScrollPane3.setViewportView(jTSciezkiTesty);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenu1.setText("Plik");

        jMenuItem1.setText("Wczytaj graf z pliku");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Zapisz wynik do pliku");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem4.setText("Zapisz graf do pliku");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem3.setText("Edytuj definicje i uzycia");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel1, 0, 169, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jbTworzGraf))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbTworzGraf)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(78, 78, 78))
        );

        pack();
    }//GEN-END:initComponents

    private void jbTworzGrafActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbTworzGrafActionPerformed
        // TODO add your handling code here:
        if (jtaKrawedzie.getText().trim().isEmpty() || jtfPocz.getText().trim().isEmpty()
                || jtfKon.getText().trim().isEmpty()) {
            jtaWynik.setText("Dane zle wprowadzone");
            return;
        }
        graf = new Graf();
        widokGrafu.reset();
        //tworzenie wierzcholkow i krawedzi
        int totalLines = jtaKrawedzie.getLineCount();
        String tekst = jtaKrawedzie.getText();
        for (int i = 0; i < totalLines; ++i) {
            try {
                //czytanie
                String linia = tekst.substring(jtaKrawedzie.getLineStartOffset(i), jtaKrawedzie.getLineEndOffset(i));
                if (linia.trim().equals("")) {
                    break;
                }
                if (linia.endsWith("\n")) {
                    linia = linia.substring(0, linia.length() - 1);
                }
                String[] daneS = linia.split(" ");
                if (daneS.length != 2) {
                    jtaWynik.setText("Dane zle wprowadzone");
                    widokGrafu.reset();
                    return;
                }
                int pocz, kon;
                try {
                    pocz = new Integer(daneS[0]);
                    kon = new Integer(daneS[1]);
                } catch (NumberFormatException e) {
                    jtaWynik.setText("Dane zle wprowadzone\nWpisz liczby jako oznaczenia\nwierzcholkow krawedzi");
                    return;
                }
                //wsadzanie do grafu
                Wierzcholek wK = graf.dodajWierzcholek(kon, widokGrafu);
                graf.dodajWierzcholek(pocz, wK, widokGrafu);
            } catch (BadLocationException ex) {
                Logger.getLogger(Formatka.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //tworzenie poczatkowych
        String linia = jtfPocz.getText();
        String[] daneS = linia.trim().split(" ");
        for (String s : daneS) {
            int l;
            try {
                l = new Integer(s);
            } catch (NumberFormatException e) {
                jtaWynik.setText("Zly format danych\nPunkty poczatkowe");
                widokGrafu.reset();
                return;
            }
            graf.dodajPocz(l, widokGrafu);
        }
        //tworzenie koncowych
        String linia2 = jtfKon.getText();
        String[] daneS2 = linia2.trim().split(" ");
        for (String s : daneS2) {
            int l_;
            try {
                l_ = new Integer(s);
            } catch (NumberFormatException e) {
                jtaWynik.setText("Zly format danych\nPunkty koncowe");
                widokGrafu.reset();
                return;
            }
            graf.dodajKon(l_, widokGrafu);
        }

        try {
            graf.sprawdzPoprawnosc();
            //wypisanie
            wypiszGraf();
            widokGrafu.paint(widokGrafu.getGraphics());
        } catch (BladPoczatku ex) {
            jtaWynik.setText("Nastapil blad w wierzcholkach poczatkowych");
        } catch (BladKonca ex) {
            jtaWynik.setText("Nastapil blad w wierzcholkach koncowych");
        } catch (BladGrafu ex) {
            jtaWynik.setText("Nastapil inny blad grafu");
        }

        this.aktywujPrzyciski(true);
    }//GEN-LAST:event_jbTworzGrafActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        WierzcholkoweAlgorytm wAlg = new WierzcholkoweAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = wAlg.tworzPokrycie();
        int ileZostalo = zbiorSciezek.get(zbiorSciezek.size() - 1).pierwszy().getNumer();
        if (ileZostalo > 0) {
            jTSciezkiTesty.append("Ponizsze wierzcholki nie maja polaczenia:\n");
            for (int i = zbiorSciezek.size() - ileZostalo - 1; i < zbiorSciezek.size() - 1; ++i) {
                Sciezka s = zbiorSciezek.get(i);
                jTSciezkiTesty.append("(" + s.pierwszy().getNumer() + ")\n");
            }
            jTSciezkiTesty.append("\n");
        }
        jTSciezkiTesty.append("Te wierzcholki powinny byc pokryte:\n TR =\n");
        for (int i = 0; i < zbiorSciezek.size() - 1; ++i) {
            if (i != zbiorSciezek.size() - ileZostalo - 2) {
                Sciezka s2 = zbiorSciezek.get(i);
                jTSciezkiTesty.append("[" + s2.pierwszy().getNumer() + "]\n");
            }
        }

        TestyWierzcholkoweAlgorytm testWAlg = new TestyWierzcholkoweAlgorytm(graf);
        testWAlg.tworzPokrycie();

        jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
        for (Sciezka s : testWAlg.getTesty()) {
            jTSciezkiTesty.append("[");
            for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");

            }

            jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        KrawedzioweAlgorytm kAlg = new KrawedzioweAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = kAlg.tworzPokrycie();
        int ileZostalo = zbiorSciezek.get(zbiorSciezek.size() - 1).pierwszy().getNumer();
        if (ileZostalo > 0) {
            jTSciezkiTesty.append("Ponizsze krawedzie sa nieodwiedzalne:\n");
            for (int i = zbiorSciezek.size() - ileZostalo - 1; i < zbiorSciezek.size() - 1; ++i) {
                Sciezka s = zbiorSciezek.get(i);
                jTSciezkiTesty.append("(" + s.pierwszy().getNumer() + ", " + s.ostatni().getNumer() + ")\n");
            }
            jTSciezkiTesty.append("\n");
        }
        jTSciezkiTesty.append("Te krawedzie powinny byc pokryte:\n TR =\n");
        for (int i = 0; i < zbiorSciezek.size() - 1; ++i) {
            if (i != zbiorSciezek.size() - ileZostalo - 2) {
                Sciezka s2 = zbiorSciezek.get(i);
                jTSciezkiTesty.append("[" + s2.pierwszy().getNumer() + ", " + s2.ostatni().getNumer() + "]\n");
            }
        }
        TestyKrawedzioweAlgorytm testKAlg = new TestyKrawedzioweAlgorytm(graf);
        testKAlg.tworzPokrycie();
        jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
        for (Sciezka s : testKAlg.getTesty()) {
            jTSciezkiTesty.append("[");
            for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
            }
            jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        FileDialog fd = new FileDialog(this, "Wczytaj graf z pliku", FileDialog.LOAD);
        fd.setVisible(true);
        String sciezka = fd.getDirectory() + fd.getFile();

        try {
            wczytajGraf(sciezka);
            if(this.graf.getWierzcholki().isEmpty()){
                jtaWynik.setText("Brak wierzcholkow w pliku");
                return;
            }
            this.aktywujPrzyciski(true);
        } catch (BladPoczatku ex) {
            jtaWynik.setText("Nastapil blad w wierzcholkach poczatkowych");
        } catch (BladKonca ex) {
            jtaWynik.setText("Nastapil blad w wierzcholkach koncowych");
        } catch (BladGrafu ex) {
            jtaWynik.setText("Nastapil inny blad grafu");
        } catch (IOException ex) {
            Logger.getLogger(Formatka.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        FileDialog fd = new FileDialog(this, "Zapisz wyniki do pliku", FileDialog.SAVE);
        fd.setVisible(true);
        String sciezka = fd.getDirectory() + fd.getFile();
        try {
            zapiszWynikDoPliku(sciezka);
        } catch (IOException ex) {
            Logger.getLogger(Formatka.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        ParKrawedzioweAlgorytm pkAlg = new ParKrawedzioweAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = pkAlg.tworzPokrycie();
        wypiszWynik(zbiorSciezek);
        TestyParKrawedzioweAlgorytm testParKAlg = new TestyParKrawedzioweAlgorytm(graf);
        testParKAlg.tworzPokrycie();
        jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
        for (Sciezka s : testParKAlg.getTesty()) {
            jTSciezkiTesty.append("[");
            for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
            }
            jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        SciezkiProsteAlgorytm spa = new SciezkiProsteAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = spa.tworzPokrycie();
        wypiszWynik(zbiorSciezek);

        TestySciezkiProsteAlgorytm testScPrAlg = new TestySciezkiProsteAlgorytm(graf);
        testScPrAlg.tworzPokrycie();
        jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
        for (Sciezka s : testScPrAlg.getTesty()) {
            jTSciezkiTesty.append("[");
            for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
            }
            jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        SciezkiDoskonaleAlgorytm sda = new SciezkiDoskonaleAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = sda.tworzPokrycie();
        wypiszWynik(zbiorSciezek);

        TestySciezkiDoskonaleAlgorytm testScDosAlg = new TestySciezkiDoskonaleAlgorytm(graf);
        testScDosAlg.tworzPokrycie();
        jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
        for (Sciezka s : testScDosAlg.getTesty()) {
            jTSciezkiTesty.append("[");
            for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
            }
            jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:

        defuse.ladujDane();
        defuse.setVisible(true);

    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        DefAlgorytm da = new DefAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = da.tworzPokrycie();
        wypiszWynik(zbiorSciezek);

        TestyDefAlgorytm testDefAlg = new TestyDefAlgorytm(graf);
        testDefAlg.tworzPokrycie();
        if (testDefAlg.getTesty() == null) {
            jTSciezkiTesty.append("Brak testow");
        } else {
            jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
            for (Sciezka s : testDefAlg.getTesty()) {
                jTSciezkiTesty.append("[");
                for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                    jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
                }
                jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
            }
        }

    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        UzycieAlgorytm ua = new UzycieAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = ua.tworzPokrycie();
        wypiszWynik(zbiorSciezek);

        TestyUzycieAlgorytm testUseAlg = new TestyUzycieAlgorytm(graf);
        testUseAlg.tworzPokrycie();
        if (testUseAlg.getTesty() == null) {
            jTSciezkiTesty.append("Brak testow");
        } else {
            jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
            for (Sciezka s : testUseAlg.getTesty()) {
                jTSciezkiTesty.append("[");
                for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                    jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
                }
                jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
            }
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        FileDialog fd = new FileDialog(this, "Zapisz graf do pliku", FileDialog.SAVE);
        fd.setVisible(true);
        String sciezka = fd.getDirectory() + fd.getFile();
        try {
            zapiszGrafDoPliku(sciezka);
        } catch (IOException ex) {
            Logger.getLogger(Formatka.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        jTSciezkiTesty.setText("");
        DuParyAlgorytm dpa = new DuParyAlgorytm(graf);
        ArrayList<Sciezka> zbiorSciezek = dpa.tworzPokrycie();
        if (zbiorSciezek.isEmpty()) {
            jTSciezkiTesty.append("Te pary miejsc powinny byc pokryte:\n TR =\nBrak");
        } else {
            jTSciezkiTesty.append("Te pary miejsc powinny byc pokryte:\n TR =\n");
            for (Sciezka s : zbiorSciezek) {
                boolean przecinek = false;
                boolean przecinek2 = false;
                for (Wierzcholek w : s.getNaSciezce()) {

                    if (w.getNumer() == -1) {
                        jTSciezkiTesty.append("), (");
                        przecinek2 = true;
                    } else {
                        if (przecinek2) {
                            przecinek2 = false;
                        } else if (przecinek) {
                            jTSciezkiTesty.append(", ");
                        } else {
                            jTSciezkiTesty.append("[(");
                            przecinek = true;
                        }
                        jTSciezkiTesty.append(w.getNumer() + "");

                    }
                }
                jTSciezkiTesty.append(")]\n");
            }
        }
        TestyDuParyAlgorytm testDuParyAlg = new TestyDuParyAlgorytm(graf);
        testDuParyAlg.tworzPokrycie();
        if (testDuParyAlg.getTesty() == null) {
            jTSciezkiTesty.append("Nie mozna utworzyc sciezek testowych\nNiektore wymagania niemozliwe do pokrycia.");
        } else {
            jTSciezkiTesty.append("Sc. testowe pokrywajace wymagania:\n T =\n");
            for (Sciezka s : testDuParyAlg.getTesty()) {
                jTSciezkiTesty.append("[");
                for (int i = 0; i < s.dlugoscSciezki() - 1; ++i) {
                    jTSciezkiTesty.append(s.podajZPozycji(i).getNumer() + ", ");
                }
                jTSciezkiTesty.append(s.ostatni().getNumer() + "]\n");
            }
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Formatka().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTSciezkiTesty;
    private javax.swing.JButton jbTworzGraf;
    private javax.swing.JTextArea jtaKrawedzie;
    private javax.swing.JTextArea jtaWynik;
    private javax.swing.JTextField jtfKon;
    private javax.swing.JTextField jtfPocz;
    // End of variables declaration//GEN-END:variables

    //wypisuje graf tekstowo
    public void wypiszGraf() {
        jtaWynik.setText("Wierzcholki i krawedzie\n");
        jtaKrawedzie.setText("");
        for (Wierzcholek w : graf.getWierzcholki()) {
            jtaWynik.append(new Integer(w.getNumer()).toString() + " - ");
            for (WierzcholekPlusPlus ww : w.getSasiedzi()) {
                jtaWynik.append(new Integer(ww.getWierzcholek().getNumer()).toString() + ", ");
                jtaKrawedzie.append(new Integer(w.getNumer()).toString() + " "
                        + new Integer(ww.getWierzcholek().getNumer()).toString() + "\n");
            }
            jtaWynik.append("\n");
        }
        jtaWynik.append("Poczatkowe\n");
        jtfPocz.setText("");
        for (Wierzcholek w : graf.getPoczatkowe()) {
            jtaWynik.append(w.getNumer() + ", ");
            jtfPocz.setText(jtfPocz.getText() + new Integer(w.getNumer()).toString()+" ");
        }
        jtaWynik.append("\nKoncowe\n");
        jtfKon.setText("");
        for (Wierzcholek w : graf.getKoncowe()) {
            jtaWynik.append(w.getNumer() + ", ");
            jtfKon.setText(jtfKon.getText() + new Integer(w.getNumer()).toString()+" ");
        }
    }

    private void wczytajGraf(String sciezka) throws IOException, BladGrafu {
        graf = new Graf();
        FileReader fr = new FileReader(sciezka);
        BufferedReader br = new BufferedReader(fr);
        String rodzaj = br.readLine();
        while (rodzaj != null) {
            rodzaj = czytajOdpowiednie(br, rodzaj);
            if (rodzaj != null) {
                if (rodzaj.equals("zleDane")) {
                    jtaWynik.setText("Dane zle wprowadzone");
                    return;
                }
            }
        }
        //sprawdz czy graf jest ok!!!!!!!!!!
        //jesli nie to rzuc wyjatek bledu
        graf.sprawdzPoprawnosc();

        //wypisanie
        wypiszGraf();
        widokGrafu.paint(widokGrafu.getGraphics());
    }

    private String czytajOdpowiednie(BufferedReader br, String rodzaj) throws IOException {
        if ("d".equals(rodzaj)) {
            return czytajKrawedzie(br);
        } else if ("p".equals(rodzaj)) {
            return czytajPoczatkowe(br);
        } else if ("k".equals(rodzaj)) {
            return czytajKoncowe(br);
        } else if ("uk".equals(rodzaj)) {
            return czytajUzyciaKrawedzi(br);
        } else if ("uw".equals(rodzaj)) {
            return czytajUzyciaWierzcholkow(br);
        } else if ("dk".equals(rodzaj)) {
            return czytajDefinicjeKrawedzi(br);
        } else if ("dw".equals(rodzaj)) {
            return czytajDefinicjeWierzcholkow(br);
        } else {
            return null;
        }
    }

    boolean czyLiterkaWPliku(String dane) {
        return !dane.equals("p") && !dane.equals("k") && !dane.equals("d")
                && !dane.equals("dk") && !dane.equals("uk")
                && !dane.equals("dw") && !dane.equals("uw");
    }

    private String czytajKrawedzie(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            String[] tab = dane.split(" ");
            System.out.println(tab[0] + "  " + tab[1]);
            if (tab.length != 2) {
                return "zleDane";
            }
            Wierzcholek wK = graf.dodajWierzcholek(new Integer(tab[1]), widokGrafu);
            graf.dodajWierzcholek(new Integer(tab[0]), wK, widokGrafu);
            dane = br.readLine();
        }
        return dane;
    }

    private String czytajPoczatkowe(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            graf.dodajPocz(new Integer(dane), widokGrafu);
            dane = br.readLine();
        }
        return dane;
    }

    private String czytajKoncowe(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            graf.dodajKon(new Integer(dane), widokGrafu);
            dane = br.readLine();
        }
        return dane;
    }

    private void zapiszWynikDoPliku(String sciezka) throws IOException {
        BufferedWriter out = new BufferedWriter(new FileWriter(sciezka));
        //FileWriter fw = new FileWriter(sciezka);
        out.write(jtaWynik.getText());
        out.newLine();
        out.append(jTSciezkiTesty.getText());
        out.close();
    }

    private void zapiszGrafDoPliku(String sciezka) throws IOException {
        BufferedWriter out = new BufferedWriter(new FileWriter(sciezka));
        //FileWriter fw = new FileWriter(sciezka);
        out.write("d");
        out.newLine();
        out.write(this.jtaKrawedzie.getText());
        out.newLine();
        out.write("p");
        out.newLine();
        out.append(this.jtfPocz.getText());
        out.newLine();
        out.write("k");
        out.newLine();
        out.append(this.jtfKon.getText());
        out.newLine();
        //tu jeszcze dodawanie do pliku definicji i uzyc jesli tylko istnieja
        for (Wierzcholek w : this.graf.getWierzcholki()) {
            List<DefUse> list = w.getDefUse();
            for (int i = 0; i < list.size(); ++i) {
                if (list.get(i) instanceof Definicja) {
                    out.write("dw");
                    out.newLine();
                    out.write(w.getNumer() + " " + list.get(i).getZmienna());
                    out.newLine();
                } else if (list.get(i) instanceof Uzycie) {
                    out.write("uw");
                    out.newLine();
                    out.write(w.getNumer() + " " + list.get(i).getZmienna());
                    out.newLine();
                }
            }
            for (WierzcholekPlusPlus wP : w.getSasiedzi()) {
                List<DefUse> list2 = wP.getDefUse();
                for (int i = 0; i < list2.size(); ++i) {
                    if (list2.get(i) instanceof Definicja) {
                        out.write("dk");
                        out.newLine();
                        out.write(w.getNumer() + " " + wP.getWierzcholek().getNumer() + " " + list2.get(i).getZmienna());
                        out.newLine();
                    } else if (list.get(i) instanceof Uzycie) {
                        out.write("uk");
                        out.newLine();
                        out.write(w.getNumer() + " " + wP.getWierzcholek().getNumer() + " " + list2.get(i).getZmienna());
                        out.newLine();
                    }
                }
            }
        }
        out.close();
    }

    private void aktywujPrzyciski(boolean aktywuj) {
        if (aktywuj) {
            this.jButton1.setEnabled(true);
            this.jButton2.setEnabled(true);
            this.jButton3.setEnabled(true);
            this.jButton4.setEnabled(true);
            this.jButton5.setEnabled(true);
            this.jButton6.setEnabled(true);
            this.jButton7.setEnabled(true);
            this.jButton8.setEnabled(true);
            jMenuItem2.setEnabled(true);
            jMenuItem3.setEnabled(true);
            jMenuItem4.setEnabled(true);
        } else {
            this.jButton1.setEnabled(false);
            this.jButton2.setEnabled(false);
            this.jButton3.setEnabled(false);
            this.jButton4.setEnabled(false);
            this.jButton5.setEnabled(false);
            this.jButton6.setEnabled(false);
            this.jButton7.setEnabled(false);
            this.jButton8.setEnabled(false);
            jMenuItem2.setEnabled(false);
            jMenuItem3.setEnabled(false);
            jMenuItem4.setEnabled(false);
        }
    }

    private String czytajUzyciaKrawedzi(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            String[] tab = dane.split(" ");

            Wierzcholek w = graf.dodajWierzcholek(new Integer(tab[0]), getWidokGrafu());
            for (int i = 2; i < tab.length; ++i) {
                w.dodajUzycie(new Integer(tab[1]), tab[i]);
            }

            dane = br.readLine();
        }
        return dane;
    }

    private String czytajUzyciaWierzcholkow(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            String[] tab = dane.split(" ");

            Wierzcholek w = graf.dodajWierzcholek(new Integer(tab[0]), getWidokGrafu());
            for (int i = 1; i < tab.length; ++i) {
                w.dodajUzycie(tab[i]);
            }

            dane = br.readLine();
        }
        return dane;
    }

    private String czytajDefinicjeKrawedzi(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            String[] tab = dane.split(" ");

            Wierzcholek w = graf.dodajWierzcholek(new Integer(tab[0]), getWidokGrafu());
            for (int i = 2; i < tab.length; ++i) {
                w.dodajDefinicje(new Integer(tab[1]), tab[i]);
                graf.getDefinicje().add(tab[i]);
            }

            dane = br.readLine();
        }
        return dane;
    }

    private String czytajDefinicjeWierzcholkow(BufferedReader br) throws IOException {
        String dane = br.readLine();
        while (dane != null && czyLiterkaWPliku(dane)) {
            String[] tab = dane.split(" ");

            Wierzcholek w = graf.dodajWierzcholek(new Integer(tab[0]), getWidokGrafu());
            for (int i = 1; i < tab.length; ++i) {
                w.dodajDefinicje(tab[i]);
                graf.getDefinicje().add(tab[i]);
            }

            dane = br.readLine();
        }
        return dane;
    }

    private void wypiszWynik(ArrayList<Sciezka> zbiorSciezek) {
        jTSciezkiTesty.append("Te wymagania powinny byc pokryte:\n TR =\n");
        if (zbiorSciezek.isEmpty()) {
            jTSciezkiTesty.append("Brak wierzcholkow");
        } else {
            for (Sciezka s : zbiorSciezek) {
                boolean przecinek = false;
                for (Wierzcholek w : s.getNaSciezce()) {
                    if (przecinek) {
                        jTSciezkiTesty.append(", ");
                    } else {
                        jTSciezkiTesty.append("[");
                    }
                    jTSciezkiTesty.append(w.getNumer() + "");
                    przecinek = true;
                }
                jTSciezkiTesty.append("]\n");
            }
        }
    }
}
